package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.ApplicationForSupervisorDao;
import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisor;
import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisorEntry;
import test.BysjException;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ApplicationForSupervisorService {
    private static ApplicationForSupervisorDao applicationForSupervisorDao
            = ApplicationForSupervisorDao.getInstance();
    private static ApplicationForSupervisorService applicationForSupervisorService
            =new ApplicationForSupervisorService();
    private ApplicationForSupervisorService(){}

    public static ApplicationForSupervisorService getInstance(){
        return applicationForSupervisorService;
    }

    public boolean add(List<ApplicationForSupervisor> applicationForSupervisors) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();

        Statement statement = connection.createStatement();
        //将自动提交设为false，开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            if(applicationForSupervisors.size()<=6) {
                statement.execute("DELETE FROM ApplicationForSupervisorEntry");
                statement.execute("DELETE FROM ApplicationForSupervisor");
                for (ApplicationForSupervisor c : applicationForSupervisors) {
                    added = applicationForSupervisorDao.add(c, connection);
                }
            }else{
                return added;
            }

            //提交事务
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务中所有操作
            connection.rollback();
            //抛出
            throw new BysjException("未能同时添加所有志愿!");
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(statement,connection);
        }
        return added;
    }

    public static void main(String[] args) throws SQLException{
        List<ApplicationForSupervisor> applicationForSupervisors = new ArrayList<ApplicationForSupervisor>();

        List<ApplicationForSupervisorEntry> applicationForSupervisorEntries =new ArrayList<ApplicationForSupervisorEntry>();

        ApplicationForSupervisorEntry applicationForSupervisores =
                new ApplicationForSupervisorEntry(TeacherService.getInstance().find(2),1,null);
        ApplicationForSupervisorEntry applicationForSupervisores2 =
                new ApplicationForSupervisorEntry(TeacherService.getInstance().find(2),2,null);

        ApplicationForSupervisor applicationForSupervisor =
                new ApplicationForSupervisor(null,"123",StudentService.getInstance().find(2),2020);
        applicationForSupervisores.setApplicationForSupervisor(applicationForSupervisor);
        applicationForSupervisores2.setApplicationForSupervisor(applicationForSupervisor);

        applicationForSupervisorEntries.add(applicationForSupervisores);
        applicationForSupervisorEntries.add(applicationForSupervisores2);
        applicationForSupervisor.setApplicationForSupervisorEntries(applicationForSupervisorEntries);
        applicationForSupervisors.add(applicationForSupervisor);

        try {
            ApplicationForSupervisorService.getInstance().add(applicationForSupervisors);
        }catch (BysjException e){
            System.out.println( e.getMessage());
        }
    }
}
